public class HappyNumber {
    public boolean isHappy(int n) {
        int slow = n, fast = n;
        do {
            slow = quadraticSum(slow);
            fast = quadraticSum(quadraticSum(fast));
        }while (slow != fast);

        return slow == 1;
    }

    public int quadraticSum(int n) {
        int sum = 0;
        while (n != 0) {
            sum += Math.pow(n % 10, 2);
            n /= 10;
        }
        return sum;
    }
}
